package com.gxa.weixing.pc.service.impl;

import com.gxa.weixing.core.util.DayUtil;
import com.gxa.weixing.pc.mapper.WorkBenchMapper;
import com.gxa.weixing.pc.service.WorkBenchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WorkBenchServiceImpl
 * @Author: ws
 * @date 2022/1/10 10:28
 */
@Service("workBenchService")
public class WorkBenchServiceImpl implements WorkBenchService {

    @Autowired
    WorkBenchMapper workbenchMapper;


    @Override
    public Map<String, Long> getBusinessData() {
        // 最终返回装有统计营销数据的map
        Map<String,Long> businessData = new HashMap<>(5);

        // 当前商品总量
        Long goodsTotal = workbenchMapper.getGoodsTotal();
        businessData.put("goodsTotal",goodsTotal);

        // 用户总量(注册用户)
        Long userTotal = workbenchMapper.getUserTotal();
        businessData.put("userTotal",userTotal);

        // 订单总量(已付款)
        Long orderTotal = workbenchMapper.getOrderTotal();
        businessData.put("orderTotal",orderTotal);

        // 当前兑换商品总量
        Long exchangeGoodsTotal = 0L;
        List<Map<String, Integer>> exchangeList = workbenchMapper.getExchangeGoodsCount();
        for (Map<String, Integer> map : exchangeList) {
            exchangeGoodsTotal += map.get("count");
        }
        businessData.put("exchangeGoodsTotal",exchangeGoodsTotal);

        // 兑换订单总量
        Long exchangeOrderTotal = workbenchMapper.getExchangeOrdersTotal();
        businessData.put("exchangeOrderTotal",exchangeOrderTotal);

        return businessData;
    }

    @Override
    public Map<String, Object> getMarketData() {
        // 最终返回装有统计营销数据的map
        Map<String,Object> marketData = new HashMap<>(6);
        // 昨天的开始日期
        Timestamp yesterday = Timestamp.valueOf(DayUtil.someDay(-1));
        // 今天的开始日期
        Timestamp today = Timestamp.valueOf(DayUtil.someDay(0));
        // 明天的开始日期
        Timestamp tomorrow = Timestamp.valueOf(DayUtil.someDay(1));

        // 总销售额(元)
        Double saleroom = 0D;
        List<Map> payPriceByOrderTime = workbenchMapper.getPayPriceByOrderTime(null, null);
        for (Map map : payPriceByOrderTime) {
             saleroom +=  Double.parseDouble(map.get("pay").toString());

        }
        marketData.put("saleroom",saleroom);

        // 昨日销售额(元)
        Double saleroomByYesterday = 0D;
        List<Map> priceByOrderTime = workbenchMapper.getPayPriceByOrderTime(yesterday, today);
        for (Map map : priceByOrderTime) {
            saleroomByYesterday += Double.parseDouble(map.get("pay").toString());
        }
        marketData.put("saleroomByYesterday",saleroomByYesterday);

        // 今日新增用户数
        Long userTotalByToday = workbenchMapper.getUserTotalByRegTime(today, tomorrow);
        marketData.put("userTotalByToday",userTotalByToday);

        // 昨日新增用户数
        Long userTotalByYesterday = workbenchMapper.getUserTotalByRegTime(yesterday, today);
        marketData.put("userTotalByYesterday",userTotalByYesterday);

        // 今日下单用户数
        List<Map> userByYesterday = workbenchMapper.getUserByOrderTime(today, tomorrow);
        int userTotalByYesterdayOrder = userByYesterday.size();
        marketData.put("userTotalByYesterdayOrder",userTotalByYesterdayOrder);

        // 昨日下单用户数
        List<Map> userByToday = workbenchMapper.getUserByOrderTime(yesterday, today);
        int userTotalByTodayOrder = userByToday.size();
        marketData.put("userTotalByTodayOrder",userTotalByTodayOrder);

        return marketData;
    }
}
